package com.jingdianjichi.user.service.impl;

import com.jingdianjichi.common.PageResponse;
import com.jingdianjichi.user.convert.SysUserConverter;
import com.jingdianjichi.user.entity.po.SysUserPo;
import com.jingdianjichi.user.mapper.SysUserDao;
import com.jingdianjichi.user.entity.req.SysUserReq;
import com.jingdianjichi.user.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2024-12-06 20:22:16
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @Cacheable(cacheNames = "SysUser", key = "'querySysUserById'+#id")
    public SysUserPo queryById(Long id) {
        return sysUserDao.queryById(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageResponse<SysUserPo> queryByPage(SysUserReq sysUserReq) {
        SysUserPo sysUserPo = SysUserConverter.INSTANCE.convertReqToPo(sysUserReq);
        PageResponse<SysUserPo> pageResponse = new PageResponse<>();
        pageResponse.setCurrent(sysUserReq.getPageNo());
        pageResponse.setPageSize(sysUserReq.getPageSize());

        Long pageStart = pageResponse.getPageStart();
        long total = sysUserDao.count(sysUserPo);
        List<SysUserPo> sysUserList = sysUserDao.queryAllByLimit(sysUserPo, pageStart, sysUserReq.getPageSize());
        pageResponse.setTotal(total);
        pageResponse.setRecords(sysUserList);
        return pageResponse;
    }

    /**
     * 新增数据
     */
    @Override
    public SysUserPo insert(SysUserReq sysUserReq) {
        SysUserPo sysUserPo = SysUserConverter.INSTANCE.convertReqToPo(sysUserReq);
        sysUserDao.insert(sysUserPo);
        return sysUserPo;
    }

    /**
     * 修改数据
     */
    @Override
    public SysUserPo update(SysUserReq sysUserReq) {
        SysUserPo sysUserPo = SysUserConverter.INSTANCE.convertReqToPo(sysUserReq);
        sysUserDao.update(sysUserPo);
        return this.queryById(sysUserPo.getId());
    }

    /**
     * 通过主键删除数据
     */
    @Override
    public boolean deleteById(Long id) {
        return sysUserDao.deleteById(id) > 0;
    }

    @Override
    public Long queryCount(Map<String, Object> conditions) {
        SysUserPo sysUserPo = new SysUserPo();
        return sysUserDao.count(sysUserPo);
    }
}
